package Demo0;
import java.io.*;
public class FileTest1 {
    public static void main(String[] args) {
        String content="";
        FileOutputStream fos=null;
        try {
            fos=new FileOutputStream("D:\\Doc\\test.txt",true);
            System.out.println("文件已经生成");
            byte[] data=content.getBytes("UTF-8");
            fos.write(data);
            fos.flush();
            System.out.println("保存内容成功");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(null!=fos){
                try {
                    fos.close();
                    fos=null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        FileInputStream fis = null;
        try {
            fis=new FileInputStream("D:\\Doc\\test.txt");
            byte[] buffer=new byte[1024];
            int len=-1;
            while((len=fis.read(buffer))!=-1){
                String str=new String(buffer,0,len);
                System.out.println(str);
            }
            System.out.println("所有内容读取完成");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            if(fis!=null){
                try {
                    fis.close();
                    fis=null;
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }
    }
}
